package com.ip.student.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ip.common.constant.Result;
import com.ip.common.enums.CodeEnum;
import com.ip.common.utils.StrUtils;
import com.ip.user.models.common.annotation.RequiresPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ip.student.models.service.IBaseSchoolService;
import com.ip.student.models.bo.BaseSchoolSaveBO;
import com.ip.student.models.bo.BaseSchoolBO;
import com.ip.student.models.dto.BaseSchoolDTO;

/**
 * 学校信息控制器
 *
 * @author 张景奇
 * @Date 2025-10-29 09:02:18
 */

@RestController
@RequestMapping("/cms/baseSchool")
@Slf4j
@Api(value = "BaseSchoolController",tags = "学校信息接口")
public class BaseSchoolController {

    @DubboReference
    private IBaseSchoolService baseSchoolService;

    @ApiOperation(value = "添加学校信息",notes = "添加学校信息")
    @PostMapping(value = "/insert", consumes = "application/json")
    @RequiresPermission("base:class:menu")
    public Result insert(@RequestBody BaseSchoolSaveBO saveBO) {
        boolean flag = baseSchoolService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存学校信息",notes = "保存学校信息")
    @RequiresPermission("base:class:menu")
    public Result save(@RequestBody BaseSchoolSaveBO saveBO){
        if(saveBO.getSchoolId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改学校信息",notes = "修改学校信息")
    @PostMapping("/update")
    @RequiresPermission("base:class:menu")
    public Result update(@RequestBody BaseSchoolSaveBO saveBO){
        BaseSchoolDTO dto = new BaseSchoolDTO();
        BeanUtils.copyProperties(saveBO, dto);
        if (saveBO.getSchoolId() != null) {
            dto.setId(Long.valueOf(saveBO.getSchoolId()));
        }
        boolean flag = baseSchoolService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除学校信息",notes = "删除学校信息")
    @PostMapping("/delete")
    @RequiresPermission("base:class:menu")
    public Result delete(@RequestBody Map<String, Object> params){
        Long id = Long.valueOf(params.get("id").toString());
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = baseSchoolService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除学校信息",notes = "批量删除学校信息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    @RequiresPermission("base:class:menu")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        baseSchoolService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    @RequiresPermission("base:class:query")
    public Result<BaseSchoolDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        BaseSchoolDTO dto = baseSchoolService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "学校信息列表", notes = "学校信息列表（支持多条件分页查询）")
    @PostMapping("/queryPageList")
    @RequiresPermission("base:class:query")
    public Result<Page<BaseSchoolDTO>> queryPageList(@RequestBody BaseSchoolBO bo) {
        // 创建查询条件包装器
        Wrapper<BaseSchoolDTO> wrapper = new EntityWrapper<>();

        // 学校ID精确查询
        if (bo.getSchoolId() != null) {
            wrapper.eq("school_id", bo.getSchoolId());
        }

        // 学校名称模糊查询
        if (StringUtils.hasText(bo.getSchoolName())) {
            wrapper.like("school_name", bo.getSchoolName());
        }

        // 学校编码精确查询（如果编码是唯一标识）或模糊查询（根据实际业务调整）
        if (StringUtils.hasText(bo.getSchoolCode())) {
            wrapper.eq("school_code", bo.getSchoolCode());
            // 若需要模糊查询可改为：wrapper.like("school_code", bo.getSchoolCode());
        }

        // 学校地址模糊查询
        if (StringUtils.hasText(bo.getAddress())) {
            wrapper.like("address", bo.getAddress());
        }

        // 校长姓名模糊查询
        if (StringUtils.hasText(bo.getPrincipal())) {
            wrapper.like("principal", bo.getPrincipal());
        }

        // 教务主任姓名模糊查询
        if (StringUtils.hasText(bo.getDean())) {
            wrapper.like("dean", bo.getDean());
        }

        // 联系电话模糊查询（支持部分号码查询）
        if (StringUtils.hasText(bo.getContactPhone())) {
            wrapper.like("contact_phone", bo.getContactPhone());
        }

        // 分页参数设置（从BaseBO继承的pageNo和pageSize）
        Page<BaseSchoolDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        // 执行分页查询
        page = baseSchoolService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}